package com.nf.health.app.utils;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * 手机工具类
 * 
 * @author shupeng
 * 
 */
public class PhoneUtils {

	/**
	 * 跳转到拨号面板
	 * 
	 * @param context
	 * @param phoneNumber
	 */
	public static void callPhone(Context context, String phoneNumber) {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_DIAL);
		intent.setData(Uri.parse("tel:" + phoneNumber));
		context.startActivity(intent);
	}

	/**
	 * 直接播打电话
	 * 
	 * @param context
	 * @param phoneNumber
	 */
	public final static void callPhone1(Context context, String phoneNumber) {
		Intent call = new Intent(Intent.ACTION_CALL);
		call.setData(Uri.parse("tel://" + phoneNumber));
		context.startActivity(call);
	}

	/**
	 * 跳转到发送短信界面
	 * 
	 * @param context
	 * @param phoneNum
	 * @param boby
	 */
	public static void sendSMS(Context context, String phoneNum, String boby) {
		Uri uri = Uri.parse("smsto:" + phoneNum);
		Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
		intent.putExtra("sms_body", boby);
		context.startActivity(intent);
	}

	/**
	 * 获得版本号
	 * 
	 * @param c
	 * @return
	 */
	public static String getVersionName(Context c) {
		PackageManager pm = c.getPackageManager();
		PackageInfo pinfo = null;
		try {
			pinfo = pm.getPackageInfo(c.getPackageName(),
					PackageManager.GET_CONFIGURATIONS);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return pinfo.versionName;
	}

	/**
	 * 获得版本code
	 * 
	 * @param c
	 * @return
	 */
	public static int getVersionCode(Context c) {
		PackageManager pm = c.getPackageManager();
		PackageInfo pinfo = null;
		try {
			pinfo = pm.getPackageInfo(c.getPackageName(),
					PackageManager.GET_CONFIGURATIONS);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return pinfo.versionCode;
	}
	
	/**
	 * 检查sd卡是否存在
	 * @return
	 */
	public static boolean checkSDCard() {
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
			return true;
		else
			return false;
	}
	
	/**
	 * 隐藏键盘
	 * @param context
	 */
	public static void hidekeyboard(Context context){
		((InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE))
		.hideSoftInputFromWindow(((Activity) context).getCurrentFocus()
		.getWindowToken(),
		InputMethodManager.HIDE_NOT_ALWAYS);
	}
	
	/**
	 * 显示键盘
	 * @param context
	 */
	public static void showkeyboard(Context context,View view){
		((InputMethodManager)context.getSystemService(context.INPUT_METHOD_SERVICE)).showSoftInput(view, 0); 
	}
	
	
    /**
     * 用来判断服务是否运行.
     * @param
     * @param className 判断的服务名字
     * @return true 在运行 false 不在运行
     */
	public static boolean isServiceRunning(Context mContext, String className) {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceList = activityManager
				.getRunningServices(30);
		if (!(serviceList.size() > 0)) {
			return false;
		}
		for (int i = 0; i < serviceList.size(); i++) {
			if (serviceList.get(i).service.getClassName().equals(className) == true) {
				isRunning = true;
				break;
			}
		}
		return isRunning;
	}
	
	/**
	 * 判断App是否在前台运行
	 * @param context
	 * @return
	 */
	public static boolean isRunningForeground(Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
		String currentPackageName = cn.getPackageName();
		if (!TextUtils.isEmpty(currentPackageName)
				&& currentPackageName.equals(context.getPackageName())) {
			return true;
		}

		return false;
	}
	
    /***
     * 判断一个Activity是否存在于栈中（是否被开启过）
     * @param cnt  上下文
     * @param clazz  要查询的Activity的字节码文件
     * @return  true 存在    ;  false 不存在 
     */
	public static boolean isExsitMianActivity(Context cnt,Class clazz){
        Intent intent = new Intent(cnt, clazz);  
        ComponentName cmpName = intent.resolveActivity(cnt.getPackageManager());  
        boolean bIsExist = false;  
        if (cmpName != null) { // 说明系统中存在这个activity  
            ActivityManager am = (ActivityManager) cnt.getSystemService(Context.ACTIVITY_SERVICE);  
            List<RunningTaskInfo> taskInfoList = am.getRunningTasks(10);  
            for (RunningTaskInfo taskInfo : taskInfoList) {  
                if (taskInfo.baseActivity.equals(cmpName)) { // 说明它已经启动了  
                    bIsExist = true;
                    break;  
                }  
            }
        }
		return bIsExist;  
    }
	/***
	 * 移除一个activity
	 * 判断一个Activity是否存在于栈中（是否被开启过）
	 * @param cnt  上下文
	 * @param clazz  要查询的Activity的字节码文件
	 * @return  true 存在    ;  false 不存在
	 *
	 */
	public static boolean removeOpenActivity(Context cnt,Class<? extends  Activity> clazz){
		Intent intent = new Intent(cnt, clazz);
		ComponentName cmpName = intent.resolveActivity(cnt.getPackageManager());
		boolean bIsExist = false;
		if (cmpName != null) { // 说明系统中存在这个activity
			ActivityManager am = (ActivityManager) cnt.getSystemService(Context.ACTIVITY_SERVICE);
			List<RunningTaskInfo> taskInfoList = am.getRunningTasks(10);
			for (RunningTaskInfo taskInfo : taskInfoList) {
				if (taskInfo.baseActivity.equals(cmpName)) { // 说明它已经启动了
					taskInfoList.remove(taskInfo);
					break;
				}
			}
		}
		return bIsExist;
	}

	/**
	 * 判断一个Activity是否处于栈顶
	 * @param mContext 
	 * @param ClassName  要判断的类的全路径
	 * @return
	 */
	public static boolean isInLauncher(Context mContext,String ClassName) {
        ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
        if(name.equals(ClassName)){
        	return true;
        }
    	return false;
    }
	/**
	 * 判断一个Activity是否处于栈顶
	 * @param mContext 
	 * @param ClassName 传一个类《class类名》
	 * @return
	 */
	public static boolean isInLauncher(Context mContext,Class<?> ClassName) {
		ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
		String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
		if(name.equals(ClassName.getName())){
			return true;
		}
		return false;
	}
}
